<template>
    <div>
        <van-nav-bar
                title="新增地址"
                placeholder
                fixed
                left-arrow
                @click-left="goBack"
        />
        <van-address-edit
                :area-list="areaList"
                show-delete
                show-postal
                show-set-default
                show-search-result
                :search-result="searchResult"
                :area-columns-placeholder="['请选择', '请选择', '请选择']"
                @save="onSave"
                @delete="onDelete"
                :address-info="{
                    name: info.name,
                    tel: info.tel,
                    addressDetail: info.addressDetail,
                    areaCode: info.areaCode,
                    postalCode: info.postalCode,
                    isDefault: info.isDefault,
                  }"
        />
    </div>
</template>

<script>
    import { areaList } from "@vant/area-data";
    import { Toast } from 'vant';

    export default {
        data() {
            return {
                areaList,
                searchResult: [],
                info: [],
                userId:''
            };
        },
        created() {
            this.EditAddress();
        },
        methods: {
            goBack() {
                this.$router.go(-1)
            },
            onSave(content) {
                console.log(content)
                //数据库该项设置为0，1，如不需修改可删除该行
                if (content.isDefault){
                    content.isDefault=1
                }else {
                    content.isDefault=0
                }
                //如果地址id不存在，则连接新增地址接口，存在则连接修改地址接口
                if (!this.$route.query.id) {
                    this.$http.post("/address/insertAddress", content).then(result => {
                        console.log(result);
                    });
                }else{
                    let id = this.$route.query.id;
                    this.$http.post("/address/updateAddressByid?id="+id,content).then(result=>{
                        console.log(result);
                    })
                }
                this.$toast.success("保存成功");
                //返回地址列表页，强制刷新
                /*this.$router.replace("/address");
                location.reload();*/
            },
            onDelete() {
                let id = this.$route.query.id;
                if (id!=null) {
                    this.$http.post("/address/delAddress?id=" + id).then(result => {
                        this.$toast.success("删除成功");
                    })
                }
            },
            EditAddress() {
                //获取地址列表传的地址id
                let id = this.$route.query.id;
                //地址id存在则发请求回显地址
                if (id!=null) {
                    console.log(id)
                    //发送请求获取地址内容
                    this.$http.post("/address/findAddressByid?id="+id).then(result=>{
                        //通过address-info回显地址/ 000000000000
                        /*this.areaList=result.data.data;*/
                        this.info = result.data.data;
                        this.info.isDefault = this.info.isDefault == 1 ? true : false;
                        console.log(this.info.isDefault);
                        console.log(this.info);

                    })
                }
            }
        },
    };
</script>

<style scoped>

</style>